package com.cn.web.controller.Equipment;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.dto.SingleRegistrationTableDto;
import com.cn.bean.pojo.lbj.*;
import com.cn.config.config.Result;
import com.cn.service.server.Equipment.SingleUpdateStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@Transactional
public class SingleUpdateStatusController {
    @Autowired
    private SingleUpdateStatusService singleUpdateStatusService;
    @RequestMapping("/select/BasicInfor")
    @ResponseBody
    public List<ComponentBasicInformationTable> selectBasicInfor(@RequestBody ComponentBasicInformationTable componentBasicInformationTable) {
        List<ComponentBasicInformationTable> assists = singleUpdateStatusService.selectBasicInformation(componentBasicInformationTable);
        return assists;
    }
    @RequestMapping("/insert/SingleUpdateStatus")
    @ResponseBody
    public Result insertSingleUpdateStatus(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
        SingleUpdateStatusTable mainTable = new SingleUpdateStatusTable();
        Integer id = singleUpdateStatusService.getSingleUpdateStatusTableAutoid();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        id = id != null ? ++id : 1;
        String userName = jsonObject.getString("userName");
        int equipmentStructureId=jsonObject.getInteger("equipmentStructureId");
        int businessProjectId=jsonObject.getInteger("businessProjectId");
        int status=jsonObject.getInteger("status");
        mainTable.setId(id);
        mainTable.setEquipmentStructureId(equipmentStructureId);
        mainTable.setBusinessProjectId(businessProjectId);
        mainTable.setStatus(status);
        mainTable.setCjr(userName);
        mainTable.setCjsj(format.format(date));

        int a = singleUpdateStatusService.insertSingleUpdateStatus(mainTable);
        if (a != 1) {
            return Result.failure("500");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<SingleUpdateStatusSubTable> djBhList = dataArray.toJavaList(SingleUpdateStatusSubTable.class);
            for (SingleUpdateStatusSubTable dataStructureDto : djBhList) {
                SingleUpdateStatusSubTable subTable = new SingleUpdateStatusSubTable();
                ComponentSingleRegistrationTable registrationTable=new ComponentSingleRegistrationTable();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                Integer autoid = singleUpdateStatusService.getSubAutoid();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMid(id);
                registrationTable.setMaterialStatus(subTable.getUpdateStatus());
                registrationTable.setUniqueCode(String.valueOf(subTable.getPartSingleId()));
                int b = singleUpdateStatusService.insertSingleUpdateStatusSub(subTable);
                int e=singleUpdateStatusService.updateComponentSingleStatus(registrationTable);
                int x=singleUpdateStatusService.openCurrent(String.valueOf(subTable.getPartSingleId()));
                if (b != 1&&e!=1) {
                    return Result.failure("500");
                }
            }
        }

        return Result.success(200);
    }
    @RequestMapping("/del/SingleStatus")
    @ResponseBody
    public Result delSingleStatus(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                singleUpdateStatusService.delStatus(id);
            }
            return Result.success(200);
        }
        return Result.failure("500", "删除失败");
    }
}
